<template>
    <el-dialog :title="$t('game.user.dialog.update')" width="400px" :visible.sync="dialogVisible" @close="$emit('update:visible', false)">
        <div class="pr-12" style="min-height: 230px">
            <el-form :model="dataCache" label-width="90px">
                <el-form-item :label="$t('game.user.role')" :error="Boolean(msg.errors['role']) ? msg.errors['role'][0] : ''" required>
                    <el-select v-model="dataCache.role" multiple>
                        <el-option v-for="(item, index) in $t('game.user.roleList')" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">{{ $t('action.save') }}</el-button>
                    <el-button @click="dialogVisible = false">{{ $t('action.cancel') }}</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: "ItemUpdateRole",
    props: ['visible', 'data'],
    data() {
        return {
            dialogVisible: false,
            dataCache: {
                id: 0,
                role: [],
            },
            msg: {
                code: 200,
                message: '',
                errors: {},
            }
        }
    },
    watch: {
        visible: function (n, o) {
            if (n) {
                // 初始化参数
                this.initData(this.data);
                // 显示模态框
                this.dialogVisible = n;
            }
        }
    },
    methods: {
        initMsg() {
            this.msg.code = 200;
            this.msg.message = '';
            this.msg.errors = {};
        },
        initData(data) {
            this.initMsg();
            this.dataCache.id = data.id;
            this.dataCache.role = data.role;
        },
        onSubmit() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/game/user/updateRole', this.dataCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.edit')}),
                        showClose: true
                    });
                    this.dialogVisible = false;
                    // 同步数据到源列表
                    this.data.role = _.cloneDeep(response.data.resp_data.role);
                } else if (_.includes([42000], response.data.resp_msg.code)) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.edit')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>

</style>
